package com.chuangjiangx.member.manager.basic.web.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by 陈智 on 2018/6/13.
 */
@Slf4j
@EnableTransactionManagement
@MapperScan(value = {
        "com.chuangjiangx.member.**.mapper",
        "com.chuangjiangx.partner.platform.dao",
//        "com.cloudrelation.partner.platform.dao",
//        "com.cloudrelation.merchant.dao",
//        "com.chuangjiangx.member.dao.dal"
//        "com.chuangjiangx.merchant",
//        "com.chuangjiangx.promote"
},nameGenerator = UniqueNameGenerator.class,sqlSessionFactoryRef = "sqlSessionFactory")
public class MybatisConfig {

    @Bean
    public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource")DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        //mybatis 配置
        Configuration configuration = new Configuration();
        configuration.setCacheEnabled(true);
        configuration.setDefaultStatementTimeout(3000);
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setUseGeneratedKeys(true);
        configuration.setAggressiveLazyLoading(true);
        sessionFactory.setConfiguration(configuration);
        //扫描mybatis的xml文件
        List<String> mapperXmlLocations = new ArrayList<>(16);
        mapperXmlLocations.add("classpath*:com/chuangjiangx/member/**/*Mapper.xml");
        mapperXmlLocations.add("classpath*:com/chuangjiangx/partner/platform/**/*Mapper.xml");
        List<Resource> resourceList = new ArrayList<>(256);
        PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();
        Resource[] resources;
        for(String url : mapperXmlLocations){
            resources = pathMatchingResourcePatternResolver.getResources(url);
            if(resources != null && resources.length > 0){
                Arrays.stream(resources).forEach(resource -> resourceList.add(resource));
            }
        }
        sessionFactory.setMapperLocations(resourceList.toArray(new Resource[resourceList.size()]));
        return sessionFactory.getObject();
    }
//    @Bean
//    public MapperScannerConfigurer mapperScannerConfigurer(){
//        StringBuilder basePackages= new StringBuilder(512);
//        //扫描mapper的Java接口
//        basePackages
//                .append("com.chuangjiangx.**.mapper").append(System.lineSeparator())
//                .append("com.cloudrelation.**.mapper").append(System.lineSeparator())
//                .append("com.chuangjiangx.**.dao").append(System.lineSeparator())
//                .append("com.cloudrelation.**.dao").append(System.lineSeparator());
//        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
//        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
//        mapperScannerConfigurer.setBasePackage(basePackages.toString());
//        mapperScannerConfigurer.setNameGenerator(new UniqueNameGenerator());
//        return mapperScannerConfigurer;
//    }
}
